﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.EditModels;
using GolfLeague.Models;

namespace GolfLeague.Controllers
{
    public class CourseController : Controller
    {
        CourseInterface courseInterface = new CourseInterface();
        ScoreCardInterface scoreCardInterface = new ScoreCardInterface();

        //
        // GET: /Course/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(new CourseEditModel
            {
                Courses = courseInterface.GetAllCourses(),
                NewCourse = new Course()
            });
        }

        //
        // POST: /Course/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Index(CourseEditModel courseEditModel)
        {
            if (ModelState.IsValid)
            {
                courseInterface.AddCourse(courseEditModel.NewCourse);
                courseInterface.Save();

                return RedirectToAction("Index");
            }
            else
            {
                courseEditModel.Courses = courseInterface.GetAllCourses();
                return View(courseEditModel);
            }
        }

        //
        // GET: /Course/CourseEdit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult CourseEdit(int id)
        {
            return View("Index", new CourseEditModel
            {
                Courses = courseInterface.GetAllCourses(),
                NewCourse = courseInterface.GetCourse(id)
            });
        }

        //
        // POST: /Course/CourseEdit/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult CourseEdit(CourseEditModel courseEditModel)
        {
            if (ModelState.IsValid)
            {
                var updateCourse = courseInterface.GetCourse(courseEditModel.NewCourse.CourseID);

                updateCourse.CourseName = courseEditModel.NewCourse.CourseName;
                updateCourse.Address = courseEditModel.NewCourse.Address;
                updateCourse.City = courseEditModel.NewCourse.City;
                updateCourse.State = courseEditModel.NewCourse.State;
                updateCourse.ZipCode = courseEditModel.NewCourse.ZipCode;

                if (TryUpdateModel(updateCourse))
                {
                    courseInterface.Save();
                    return RedirectToAction("Index");
                }
            }
            courseEditModel.Courses = courseInterface.GetAllCourses();
            return View("Index", courseEditModel);
        }

        //
        // POST: /Course/CourseDelete/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult CourseDelete(int courseID)
        {
            Course course = courseInterface.GetCourse(courseID);

            courseInterface.DeleteCourse(course);
            courseInterface.Save();

            return RedirectToAction("Index");
        }

        //
        // GET: /Course/ScoreCard/5

        [Authorize(Roles = "Administrator")]
        public ActionResult ScoreCard(int id)
        {
            return View(scoreCardInterface.GetCourseScoreCardEditModel(id));
        }

        //
        // POST: /Course/TeeBoxAdd/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult TeeBoxAdd(CourseScoreCardEditModel scoreCardEditModel)
        {
            if (ModelState.IsValid)
            {
                if (scoreCardInterface.AddTeeBox(scoreCardEditModel.NewTeeBox)) scoreCardInterface.Save();
                return RedirectToAction("ScoreCard", new { id = scoreCardEditModel.NewTeeBox.CourseID });
            }
            else
            {
                TeeBox teeBox = scoreCardEditModel.NewTeeBox;
                scoreCardEditModel = scoreCardInterface.GetCourseScoreCardEditModel(teeBox.CourseID);
                scoreCardEditModel.NewTeeBox = teeBox;
                return View("ScoreCard", scoreCardEditModel);
            }
        }

        //
        // GET: /Course/TeeBoxEdit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult TeeBoxEdit(int id)
        {
            TeeBox teeBox = scoreCardInterface.GetTeeBox(id);
            CourseScoreCardEditModel scoreCardEditModel = scoreCardInterface.GetCourseScoreCardEditModel(teeBox.CourseID);
            scoreCardEditModel.NewTeeBox = teeBox;

            return View("ScoreCard", scoreCardEditModel);
        }

        //
        // POST: /Course/TeeBoxEdit/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult TeeBoxEdit(CourseScoreCardEditModel scoreCardEditModel)
        {
            if (ModelState.IsValid)
            {
                var updateTeeBox = scoreCardInterface.GetTeeBox(scoreCardEditModel.NewTeeBox.TeeBoxID);

                updateTeeBox.TeeBoxName = scoreCardEditModel.NewTeeBox.TeeBoxName;
                updateTeeBox.Slope = scoreCardEditModel.NewTeeBox.Slope;
                updateTeeBox.Rating = scoreCardEditModel.NewTeeBox.Rating;

                if (TryUpdateModel(updateTeeBox))
                {
                    scoreCardInterface.Save();
                    return RedirectToAction("ScoreCard", new { id = scoreCardEditModel.NewTeeBox.CourseID });
                }
            }
            TeeBox teeBox = scoreCardEditModel.NewTeeBox;
            scoreCardEditModel = scoreCardInterface.GetCourseScoreCardEditModel(scoreCardEditModel.NewTeeBox.CourseID);
            scoreCardEditModel.NewTeeBox = teeBox;

            return View("ScoreCard", scoreCardEditModel);
        }

        //
        // POST: /Course/TeeBoxDelete/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult TeeBoxDelete(int id)
        {
            TeeBox teeBox = scoreCardInterface.GetTeeBox(id);

            scoreCardInterface.DeleteTeeBox(teeBox);
            scoreCardInterface.Save();

            return RedirectToAction("ScoreCard", new { id = teeBox.CourseID });
        }

        //
        // POST: /Course/HoleAdd/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult HoleAdd(CourseScoreCardEditModel scoreCardEditModel)
        {
            if (ModelState.IsValid)
            {
                if (scoreCardInterface.AddHole(scoreCardEditModel.NewHole))
                {
                    scoreCardInterface.Save();
                    scoreCardEditModel.NewHoleDetails.HoleID = scoreCardEditModel.NewHole.HoleID;
                }
                else
                {
                    scoreCardEditModel.NewHoleDetails.HoleID = scoreCardInterface.GetAllCourseHoles(scoreCardEditModel.NewHole.CourseID).Single(h => h.HoleNumber == scoreCardEditModel.NewHole.HoleNumber).HoleID;
                }
                if (scoreCardInterface.AddHoleDetails(scoreCardEditModel.NewHoleDetails))
                {
                    scoreCardInterface.Save();
                }

                return RedirectToAction("ScoreCard", new { id = scoreCardEditModel.NewHole.CourseID });
            }
            else
            {
                Hole hole = scoreCardEditModel.NewHole;
                HoleDetail holeDetails = scoreCardEditModel.NewHoleDetails;
                scoreCardEditModel = scoreCardInterface.GetCourseScoreCardEditModel(hole.CourseID);
                scoreCardEditModel.NewHole = hole;
                scoreCardEditModel.NewHoleDetails = holeDetails;
                return View("ScoreCard", scoreCardEditModel);
            }
        }

        //
        // GET: /Course/HoleEdit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult HoleEdit(int id)
        {
            Hole hole = scoreCardInterface.GetHole(id);
            CourseScoreCardEditModel scoreCardEditModel = scoreCardInterface.GetCourseScoreCardEditModel(hole.CourseID);
            scoreCardEditModel.NewHole = hole;
            scoreCardEditModel.NewHoleDetails = scoreCardInterface.GetHoleDetails(id, scoreCardEditModel.TeeBoxDropDown.First().dropdownID);

            return View("ScoreCard", scoreCardEditModel);
        }

        //
        // POST: /Course/HoleEdit/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult HoleEdit(CourseScoreCardEditModel scoreCardEditModel)
        {
            if (ModelState.IsValid)
            {
                var holeUpdate = scoreCardInterface.GetHole(scoreCardEditModel.NewHole.HoleID);
                var holeDetailsUpdate = scoreCardInterface.GetHoleDetails(scoreCardEditModel.NewHole.HoleID, scoreCardEditModel.NewHoleDetails.TeeBoxID);

                holeUpdate.HoleNumber = scoreCardEditModel.NewHole.HoleNumber;
                holeUpdate.Handicap = scoreCardEditModel.NewHole.Handicap;

                holeDetailsUpdate.Yardage = scoreCardEditModel.NewHoleDetails.Yardage;
                holeDetailsUpdate.Par = scoreCardEditModel.NewHoleDetails.Par;

                if (TryUpdateModel(holeUpdate) && TryUpdateModel(holeDetailsUpdate))
                {
                    scoreCardInterface.Save();
                    return RedirectToAction("ScoreCard", new { id = scoreCardEditModel.NewHole.CourseID });
                }
                else
                {
                    return RedirectToAction("HoleEdit", new { id = scoreCardEditModel.NewHole.HoleID });
                }

            }
            else
            {
                Hole hole = scoreCardEditModel.NewHole;
                HoleDetail holeDetails = scoreCardEditModel.NewHoleDetails;
                scoreCardEditModel = scoreCardInterface.GetCourseScoreCardEditModel(hole.CourseID);
                scoreCardEditModel.NewHole = hole;
                scoreCardEditModel.NewHoleDetails = holeDetails;

                return View("ScoreCard", scoreCardEditModel);
            }
        }

        //
        // POST: /Course/HoleDetails/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public JsonResult HoleDetails(int holeID, int teeBoxID)
        {
            HoleDetail holeDetails = scoreCardInterface.GetHoleDetails(holeID, teeBoxID);
            if (holeDetails == null) holeDetails = new HoleDetail { Par = 0, Yardage = 0 };

            return Json(new { holeDetails.Yardage, holeDetails.Par });
        }

        //
        // GET: /Course/CourseView/5

        public ActionResult CourseView(int id)
        {
            return View(courseInterface.GetCourse(id));
        }
    }
}
